package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Person;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.println("请输入您的选项：1. 添加教师  2.删除教师  3.修改教师  4.查看教师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
//                    System.out.println("添加教师");
                    addTeacher();
                    break;
                case "2":
//                    System.out.println("删除教师");
                    deleteTeacher();
                    break;
                case "3":
//                    System.out.println("修改教师");
                    updateTeacher();
                    break;
                case "4":
//                    System.out.println("查看教师");
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("感谢您使用教师管理系统，再见！");
                    break teacherLoop;
                default:
                    System.out.printf("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updateTeacher() {
        String updateId = inputTeacherId();
        teacherService.updateTeacher(inputTeacherInfo(updateId));
        System.out.println("修改成功");
    }

    private void deleteTeacher() {
        String delId = inputTeacherId();
        teacherService.deleteTeacherById(delId);
        System.out.println("删除成功");
    }

    public void findAllTeachers() {
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("查无信息，请重新添加");
            return;
        }
        System.out.println("教师id\t\t\t姓名\t年龄\t\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t\t\t" + teacher.getName() + "\t" +
                        teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入教师 id：");
            id = sc.next();
            boolean exist = teacherService.isExists(id);
            if (exist) {
                System.out.println("教师 id 已被占用，请重新输入");
                continue;
            } else {
                break;
            }
        }

        Teacher teacher = inputTeacherInfo(id);
        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入教师 id");
            id = sc.next();
            boolean exist = teacherService.isExists(id);
            if (!exist) {
                System.out.println("您输入的 id 不存在，请重新输入：");
            } else {
                break;
            }
        }

        return id;
    }

    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入教师姓名：");
        String name = sc.next();
        System.out.println("请输入教师年龄：");
        String age = sc.next();
        System.out.println("请输入教师生日：");
        String birthday = sc.next();

        Teacher teacher = new Teacher(id, name, age, birthday);
        // TODO 待优化
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);

        return teacher;
    }
}
